"use strict";
const BaseImpl = require("../basics/clz/baseimpl");
const Constant = require("../basics/constant");
const Result = require("../basics/result");
const DateUtil = require("../utils/time");
const GenUtil = require("../utils/gen");
const DictsModel = require("../model/dicts");
const TaskRecordModel = require("../model/task_record");
const GoodsModel = require("../model/goods");
const GamePowerModel = require("../model/game_power");
const UserModel = require("../model/user");

/**
 * 通用服务
 */
class HomeImpl extends BaseImpl {

  /**
   * 用户登录
   * @param {*} params 
   */
  async login(params) {
    let { openId, mixNick } = params;
    const user = await UserModel.getInstant(this.cloud).login(openId, mixNick, DateUtil.getGMT8Time());
    return { openId: user.openId, nickname: user.nickname, fraction: user.fraction, isfirst: user.isfirst }
  }

  /**
   * 用户授权
   * @param {*} params 
   */
  async auth(params) {
    let { openId, nickname } = params;
    const userModel = UserModel.getInstant(this.cloud);
    const user = await userModel.selectOneByOpenId(openId);
    return await userModel.updateById({ nickname }, user.id);
  }
  /**
   * 完成第一次
   * @param {*} params 
   */
  async finishFirst(params) {
    let { openId } = params;
    const userModel = UserModel.getInstant(this.cloud);
    const user = await userModel.selectOneByOpenId(openId);
    return await userModel.updateById({ isfirst: 0 }, user.id);
  }

  /**
   * 任务列表
   * @param {Object} params 
   */
  async findTaskList(params) {
    let { openId } = params;
    const mineList = await TaskRecordModel.getInstant(this.cloud).selectList(openId, DateUtil.getStringDay());
    let codeList = [];
    for (let i = 0; i < mineList.length; i++) {
      const { code, status } = mineList[i];
      if (status != 1) {
        continue;
      }
      codeList.push(code);
    }
    let taskList = [];
    let arrs = await DictsModel.getInstant(this.cloud).selectTaskList();
    for (let i = 0; i < arrs.length; i++) {
      let { code, name, idList } = arrs[i];
      let data = { code, name, isdone: false };
      if (codeList.includes(code)) {
        data.isdone = true;
      }
      if (idList && idList.length > 0) {
        let len = GenUtil.randomInt(idList.length);
        data.linkId = idList[len - 1];
      }
      taskList.push(data);
    }
    return taskList;
  }

  /**
   * 商品列表
   * @param {*} params 
   */
  async findGoodList(params) {
    let { type, openId } = params;
    let arrs = await GoodsModel.getInstant(this.cloud).selectList(type);
    if (type == 1) {
      let idList = [];
      const record = await TaskRecordModel.getInstant(this.cloud).selectOne(openId, DateUtil.getStringDay(), Constant.TASK.purch);
      if (record) {
        idList = JSON.parse(record.details)
      }
      for (let i = 0; i < arrs.length; i++) {
        const { id } = arrs[i];
        if (idList.length == 0 || !idList.includes(id)) {
          arrs[i].isAdd = false;
        } else {
          arrs[i].isAdd = true;
        }
      }
    }
    return arrs;
  }

  /**
   * 完成任务
   * @param {*} params 
   */
  async finishTask(params) {
    let { openId, code } = params;
    if (code == Constant.TASK.purch) {
      throw Result.error(Result.constant.fail, "不支持加购商品任务");
    }
    const today = DateUtil.getStringDay();
    const taskRecordModel = TaskRecordModel.getInstant(this.cloud);
    const record = await taskRecordModel.selectOne(openId, today, code);
    if (record) {
      throw Result.error(Result.constant.fail, "请勿重复完成");
    }
    try {
      await taskRecordModel.insertOne({ openId, today, code, details: "", status: 1 });
    } catch (error) {
      throw Result.error(Result.constant.fail, "请勿重复完成");
    }
    const gamePowerModel = GamePowerModel.getInstant(this.cloud);
    const power = await gamePowerModel.selectOne(openId, today);
    await gamePowerModel.increased(power.id);
    return true;
  }


  /**
   * 加购商品
   * @param {*} params 
   */
  async purchase(params) {
    let { openId, id } = params;
    const code = Constant.TASK.purch;
    const today = DateUtil.getStringDay();
    const taskRecordModel = TaskRecordModel.getInstant(this.cloud);
    const record = await taskRecordModel.selectOne(openId, today, code);
    if (!record) {
      try {
        await taskRecordModel.insertOne({ openId, today, code, details: JSON.stringify([id]), status: 0 });
      } catch (error) {
        throw Result.error(Result.constant.fail, "请勿频繁操作");
      }
      return false;
    }
    let ids = JSON.parse(record.details);
    if (ids.includes(id)) {
      return false;
    }
    ids.push(id);
    if (ids.length < 3) {
      await taskRecordModel.updateById({ details: JSON.stringify(ids) }, record.id);
      return false;
    }
    const count = await taskRecordModel.finishPurch(record.id, JSON.stringify(ids));
    if (count < 1) {
      throw Result.error(Result.constant.fail, "请勿重复完成");
    }
    const gamePowerModel = GamePowerModel.getInstant(this.cloud);
    const power = await gamePowerModel.selectOne(openId, today);
    await gamePowerModel.increased(power.id);
    return true;
  }

}

module.exports = HomeImpl;